package fem2d;

import math2.PulseFunctionRToR;
import fem2.AbstractStructuralStaticDemo;
import fem2.Constraint;
import fem2.Debugger;
import fem2.Force;
import fem2.Mesh;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.Observer;
import fem2.StringUtilities;
import fem2.analysis.Analysis;
import fem2.analysis.FreeVibrationAnalysis;
import fem2.enu.EchoLevelType;
import fem2.enu.MassMatrixType;
import fem2.enu.NewmarkType;
import fem2.observer.GidDisplacementObserver;
import fem2.observer.GidResistantObserver;
import fem2.observer.GidStressObserver;
import fem2.observer.TimeStepObserver;
import fem2.observer.TimingObserver;
import fem2.pre_and_post.GidPostStructural;
import fem2.strategies.Strategy;

/**
 * Lshape with structured mesh with mesh is created by joining two meshes
 * 
 * @author hbui
 * 
 */
public class LshapeFreeVibration extends Lshape {

	public LshapeFreeVibration() {
		projectDir = "/home/hbui/kratos_janosch";
		projectName = "Lshape.gid";
		meshFileName = "Lshape-structured-freevib";
	}

	@Override
	public Model createConditions(Model m) {
		Mesh mesh = m.getMesh();
		Constraint c1 = new Constraint(false, false);
		mesh.setConstraint(c1, MeshUtilities.seekNodesOnSurface(mesh, 0, 1, -1000));

		Node n = MeshUtilities.seekNode(mesh, 1000, 500);
		Force f = new Force(n, 0, -100);
		f.setTimeFactor(new PulseFunctionRToR(0.0, 1e-6, 1.0));
		m.addLoad(f);

		return m;
	}

	@Override
	public void addObservers(Model m, Strategy s, TimeStepObserver o) {
		if (meshFileName == null) {
			throw new Error("mesh file name was not set");
		}

		/*
		 * clean all previous post files
		 */
		String meshName = StringUtilities.getFileName(meshFileName);

		GidPostStructural post = new GidPostStructural(projectDir, projectName, meshName);
		post.clearAllPostFiles();

		Observer so = new GidStressObserver(m, o, projectDir, projectName, meshName);

		// Observer ro1 = new ResistantGraphicalObserver(m, s,
		// getNodesForResistantObserver(m));
		Observer ro2 = new GidResistantObserver(m, o, projectDir, projectName, meshName);

		Observer do1 = new GidDisplacementObserver(m, o, projectDir, projectName, meshName);

		s.addObserver(so);
		// s.addObserver(ro1);
		s.addObserver(ro2);
		s.addObserver(do1);
		s.addObserver(new TimingObserver());
	}

	@Override
	public void run() {
		Model m = createModel();

		Analysis an = new FreeVibrationAnalysis(m, MassMatrixType.VARIATIONAL_MASS_LUMPING, 0.0,
				0.0); // no damping
		((FreeVibrationAnalysis) an).setAnalysisTypeToNonAdaptiveNewmark(NewmarkType.NewmarkAlpha,
				0.8, 0.0, 0.01, 50);
		Strategy s = an.getStrategy();
		// an.getStrategy().setEchoLevel(EchoLevelType.AUX2);
		s.setEchoLevel(EchoLevelType.OUTPUT, EchoLevelType.DEBUG);
		// an.getStrategy().setSolverType(SolverType.BAITSCH_SOLVER_GENERIC);

		addObservers(m, s, new TimeStepObserver(s));

		an.run();

		Debugger.warn("Analysis completed");
	}

	public static void main(String[] args) {
		AbstractStructuralStaticDemo demo = new LshapeFreeVibration();

		demo.run();

	}
}
